package com.jereh.topnews.demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.jereh.topnews.R;
import com.jereh.topnews.util.HttpUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageSaveActivity extends AppCompatActivity {

    private ImageView img;
    private Bitmap bitmap;
    private File dir=Environment.getExternalStorageDirectory();
    private String fileName="sky.jpg";
    private int imgId=R.mipmap.dog1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_save);
        img=(ImageView)findViewById(R.id.img);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存图片到本地，并展示在img上
                //判断sd卡
                if(isSDExist()) {
                    // 下载图片
//                    downLoadImg();
                    //保存应用的资源图片
                    saveLocalImg();
                }

            }
        });
    }

    private void saveLocalImg() {
        // 保存图片
        bitmap=BitmapFactory.decodeResource(getResources(),imgId);
        saveImg();
        //展示图片
        readImg();
    }

    private void downLoadImg() {
        //回到主线程
        final Runnable callback=new Runnable() {
            @Override
            public void run() {
                //图片保存
                if(saveImg()) {
                    //显示图片
                    readImg();
                }
            }
        };
        //下载图片
        Thread thread=new Thread() {
            @Override
            public void run() {
                //下载图片
                bitmap= HttpUtil.getBitmapByUrl("http://pic21.nipic.com/20120609/6999695_132010613322_2.jpg");
                //回到主线程
                runOnUiThread(callback);
            }
        };
        thread.start();
    }

    /**
     * 读取图片
     */
    private void readImg() {
        try {
            FileInputStream fis=new FileInputStream(new File(dir.getCanonicalPath(),fileName));
//            //把图像文件读取到byte数组中
//            byte[] b=new byte[fis.available()];
//            fis.read(b);
//            fis.close();
            img.setImageBitmap(BitmapFactory.decodeStream(fis));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片保存
     * @return
     */
    private boolean saveImg(){
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
        byte[] b=baos.toByteArray();
        //获取byte数组
        //文件写入
        try {
            FileOutputStream fos=new FileOutputStream(new File(dir.getCanonicalPath(),fileName));
            fos.write(b);
            fos.flush();
            fos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    private boolean isSDExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

}
